package cn.hwadee.muying.service;

import cn.hwadee.muying.dto.Worker;
import cn.hwadee.muying.entity.Comment;
import cn.hwadee.muying.mapper.CommentMapper;
import cn.hwadee.muying.commons.base.BaseService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuWuqiang
 * @since 2018-08-08
 */
@Service
public class CommentService extends BaseService<CommentMapper, Comment> {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserService userService;
    public MyPage<Comment> list(MyPage<Comment> page, User cUser,String keyword,Integer star) throws Exception{
        List<Comment> comments = commentMapper.list(page,cUser,keyword,star);
        for(Comment comment:comments){
            if(comment.getWorkerId()!=null){
                User user=userService.selectById(comment.getWorkerId());
                if(user!=null){
                    comment.setWorkername(user.getUsername());
                }
                user=userService.selectById(comment.getUserId());
                if(user!=null){
                    comment.setUsername(user.getUsername());
                }
            }
        }
        page.setRecords(comments);
        return page;
    }


    public List<Comment> selectListByWorkerId(Long workerId) {
        return commentMapper.selectList(
                new EntityWrapper<Comment>()
                        .and("worker_id="+workerId)
                        .and("valid=1")
        );
    }

    public List<Comment> selectFrontList(Long workerId){
        List<Comment> commentList=this.selectListByWorkerId(workerId);
        for(Comment comment:commentList){
            if(comment.getWorkerId()!=null){
                User user=userService.selectById(comment.getWorkerId());
                comment.setUsername(user.getUsername());
            }
        }
        if(commentList.size()>5){
            return commentList.subList(0,5);
        }else{
            return commentList;
        }
    }

}
